package com.bella.api.system.controller;

import com.bella.api.system.dto.user.AdminInfoDto;
import com.bella.api.system.dto.user.AdminQueryDto;
import com.bella.api.system.service.MenuService;
import com.bella.api.system.service.UserService;
import com.bella.api.utils.ApiResponse;
import com.bella.api.utils.ApiResponseCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


/**
 * @author zsq
 * @create 2020-04-17-10:11
 */
@Api(description = "用户操作接口")
@RestController
@RequestMapping("/web/v1/admin")
public class UserController {
    @Value("E:/")
    private String filePath;
    @Autowired
    UserService userService;
    @Autowired
    MenuService menuService;

    @ApiOperation(value = "退出登录", notes = "管理员退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ApiResponse logout() {
        userService.logout();
        return new ApiResponse(ApiResponseCode.SUCCESS);
    }

    @ApiOperation(value = "获取用户信息", notes = "根据token获取用户信息")
    @RequestMapping(value = "/getInfo/{id}", method = RequestMethod.GET)
    public ApiResponse getInfo(@PathVariable Integer id) {
        return new ApiResponse(ApiResponseCode.SUCCESS, userService.getInfo(id));
    }

    @ApiOperation(value = "获取用户列表", notes = "获取所有用户列表")
    @RequestMapping(value = "/adminList", method = RequestMethod.GET)
    public ApiResponse userList(@ModelAttribute AdminQueryDto adminQueryDto) {
        return new ApiResponse(ApiResponseCode.SUCCESS, userService.getAdminList(adminQueryDto));
    }

    @ApiOperation(value = "新增用户", notes = "新增用户")
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public ApiResponse addUser(@RequestBody AdminInfoDto adminInfoDto) {
        userService.addUser(adminInfoDto);
        return new ApiResponse(ApiResponseCode.SUCCESS);
    }

    @ApiOperation(value = "修改用户", notes = "修改用户")
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public ApiResponse updateUser(@RequestBody AdminInfoDto adminInfoDto) {
        userService.updateUser(adminInfoDto);
        return new ApiResponse(ApiResponseCode.SUCCESS);
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @RequestMapping(value = "/delUser/{id}", method = RequestMethod.POST)
    public ApiResponse delUser(@PathVariable Integer id) {
        userService.delUser(id);
        return new ApiResponse(ApiResponseCode.SUCCESS);
    }
    @ApiOperation(value = "用户菜单", notes = "用户菜单")
    @RequestMapping(value = "/nav", method = RequestMethod.GET)
    public ApiResponse nav() {
        return new ApiResponse(ApiResponseCode.SUCCESS, menuService.nav());
    }

    @ApiOperation(value = "批量上传附件", notes = "批量上传附件")
    @RequestMapping(value = "/uploadAttach", method = RequestMethod.GET)
    public ApiResponse uploadAttach(@RequestParam(value = "files", required = false) MultipartFile[] files) {
        for (MultipartFile file : files) {
            Long time = System.currentTimeMillis();
            String attachmentName = time + "_" + getRandomNum(4);
            String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String filePath = "";
            if (file.equals("") || file.getSize() <= 0) {
                file = null;
            } else {
                filePath = filePath + attachmentName + fileSuffix;
                File path = new File(filePath + attachmentName + fileSuffix);
                if (!path.getParentFile().exists()) {
                    path.getParentFile().mkdirs();
                }
                try {
                    file.transferTo(path);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return new ApiResponse(ApiResponseCode.SUCCESS, filePath);
    }

    public static String getRandomNum(int num) {
        String dataCode = "";
        for (int i = 0; i < num; i++) {
            dataCode += String.valueOf(Math.round(Math.random() * 9));
        }
        return dataCode;
    }
}
